﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TankIt.Models
{
    public class HoldEmTable : Table
    {
        public List<Seat<HoldEmPlayer>> Seats { get; set; }
        public HoldEmDeck Deck { get; set; }

        public HoldEmTable(int Seats = 9)
            : base(Seats)
        {
            this.Deck = HoldEmDeck.NewDeck();
        }

        public override void Deal()
        {
            for(int s = 0; s < (this.Seats.Count * 2); s++)
            {
                if (s < this.Seats.Count)
                {
                    if (this.Seats[s].Player != null)
                    {
                        this.Seats[s].Player.Hand.Cards.Add(this.Deck.GetNextCard());
                    }
                }
                else
                {
                    int sDiff = s - this.Seats.Count;
                    if (this.Seats[sDiff].Player != null)
                    {
                        this.Seats[sDiff].Player.Hand.Cards.Add(this.Deck.GetNextCard());
                    }
                }
            }
        }

        public override void AddSeats()
        {
            this.Seats = new List<Seat<HoldEmPlayer>>();
            for (int s = 0; s < this.TotalSeats; s++)
            {
                this.Seats.Add(new Seat<HoldEmPlayer>(s));
            }
        }

        public void SeatPlayers(List<HoldEmPlayer> Players)
        {
            if (Players.Count > this.TotalSeats)
                throw new ArgumentOutOfRangeException("Two many players for this table");

            foreach (var p in Players)
            {
                p.Hand = new Hand();
                this.Seats[Players.IndexOf(p)].SeatPlayer(p);
            }
        }
    }
}